package com.library.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.library.entity.DataDictionary;
import com.library.mapper.DataDictionaryMapper;
import com.library.service.DataDictionaryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * <p>
 * 数据字典 服务实现类
 * </p>
 *
 * @author drinkcode
 * @since 2023-04-10
 */
@Service
public class DataDictionaryServiceImpl extends ServiceImpl<DataDictionaryMapper, DataDictionary> implements DataDictionaryService {

    @Resource
    private DataDictionaryMapper dataDictionaryMapper;

    @Override
    public List<DataDictionary> queryListIndex() {
        LambdaQueryWrapper<DataDictionary> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DataDictionary::getType, "A");
        queryWrapper.orderByAsc(DataDictionary::getDictionarySort);

        return dataDictionaryMapper.selectList(queryWrapper);
    }

    @Override
    public List<DataDictionary> queryListById(Long id) {

        List<DataDictionary> dataDictionaries = new ArrayList<>();
        if (id == null){
            return dataDictionaries;
        }

        LambdaQueryWrapper<DataDictionary> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DataDictionary::getId, id);
        queryWrapper.eq(DataDictionary::getType, "A");
        DataDictionary dataDictionary = dataDictionaryMapper.selectOne(queryWrapper);
        if (dataDictionary == null){
            return dataDictionaries;
        }

        LambdaQueryWrapper<DataDictionary> queryWrapper2 = new LambdaQueryWrapper<>();
        queryWrapper2.eq(DataDictionary::getParentId, dataDictionary.getId());
        queryWrapper2.eq(DataDictionary::getType, "B");
        queryWrapper2.orderByAsc(DataDictionary::getDictionarySort);
        dataDictionaries = dataDictionaryMapper.selectList(queryWrapper2);

        return dataDictionaries;
    }

    @Override
    public List<DataDictionary> queryListByName(String name) {

        List<DataDictionary> dataDictionaries = new ArrayList<>();
        if (name == null || "".equals(name)){
            return dataDictionaries;
        }

        LambdaQueryWrapper<DataDictionary> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DataDictionary::getDictionaryName, name);
        queryWrapper.eq(DataDictionary::getType, "A");
        DataDictionary dataDictionary = dataDictionaryMapper.selectOne(queryWrapper);
        if (dataDictionary == null){
            return dataDictionaries;
        }

        LambdaQueryWrapper<DataDictionary> queryWrapper2 = new LambdaQueryWrapper<>();
        queryWrapper2.eq(DataDictionary::getParentId, dataDictionary.getId());
        queryWrapper2.eq(DataDictionary::getType, "B");
        queryWrapper2.orderByAsc(DataDictionary::getDictionarySort);
        dataDictionaries = dataDictionaryMapper.selectList(queryWrapper2);

        return dataDictionaries;
    }

    @Override
    public List<DataDictionary> queryListByCode(String code) {

        List<DataDictionary> dataDictionaries = new ArrayList<>();
        if (code == null || "".equals(code)){
            return dataDictionaries;
        }

        LambdaQueryWrapper<DataDictionary> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DataDictionary::getDictionaryCode, code);
        queryWrapper.eq(DataDictionary::getType, "A");
        DataDictionary dataDictionary = dataDictionaryMapper.selectOne(queryWrapper);
        if (dataDictionary == null){
            return dataDictionaries;
        }

        LambdaQueryWrapper<DataDictionary> queryWrapper2 = new LambdaQueryWrapper<>();
        queryWrapper2.eq(DataDictionary::getParentId, dataDictionary.getId());
        queryWrapper2.eq(DataDictionary::getType, "B");
        queryWrapper2.orderByAsc(DataDictionary::getDictionarySort);
        dataDictionaries = dataDictionaryMapper.selectList(queryWrapper2);

        return dataDictionaries;
    }

    @Override
    public boolean addDataDictionaryA(DataDictionary dataDictionary0) {

        String dictionaryName = dataDictionary0.getDictionaryName();
        String dictionaryCode = dataDictionary0.getDictionaryCode();

        if (dictionaryName == null || "".equals(dictionaryName)){
            throw new RuntimeException("请勿传入空值！");
        }
        if (dictionaryCode == null || "".equals(dictionaryCode)){
            throw new RuntimeException("请勿传入空值！");
        }

        LambdaQueryWrapper<DataDictionary> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DataDictionary::getDictionaryName, dictionaryName);
        queryWrapper.eq(DataDictionary::getType, "A");
        List<DataDictionary> list = dataDictionaryMapper.selectList(queryWrapper);
        if (list != null && list.size()>0){
            throw new RuntimeException("“" + dictionaryName + "”类型已经存在，请勿添加重复的字典类型！");
        }

        LambdaQueryWrapper<DataDictionary> queryWrapper2 = new LambdaQueryWrapper<>();
        queryWrapper2.eq(DataDictionary::getDictionaryCode, dictionaryCode);
        queryWrapper2.eq(DataDictionary::getType, "A");
        List<DataDictionary> list2 = dataDictionaryMapper.selectList(queryWrapper2);
        if (list2 != null && list2.size()>0){
            throw new RuntimeException("code“" + dictionaryCode + "”已经存在，请勿添加重复的字典code！");
        }

        DataDictionary dataDictionary = new DataDictionary();
        dataDictionary.setType("A");
        dataDictionary.setDictionaryName(dictionaryName);
        dataDictionary.setDictionaryCode(dictionaryCode);

        if (dataDictionary0.getDictionarySort() != null) {
            dataDictionary.setDictionarySort(dataDictionary0.getDictionarySort());
        }else{
            dataDictionary.setDictionarySort(1);
        }

        //dataDictionary.setOperatorId(WmsCommonUtils.getStringUserInfo(WMS_CCP_USER_INFO_USER_CODE.getCode()));
        dataDictionary.setUpdateTime(new Date());

        return this.save(dataDictionary);

    }

    @Override
    public boolean updateDataDictionaryA(DataDictionary dataDictionary) {
        String dictionaryName = dataDictionary.getDictionaryName();
        String dictionaryCode = dataDictionary.getDictionaryCode();
        if (dictionaryName == null || "".equals(dictionaryName)){
            throw new RuntimeException("请勿传入空值！");
        }
        if (dictionaryCode == null || "".equals(dictionaryCode)){
            throw new RuntimeException("请勿传入空值！");
        }
        if (dataDictionary.getId() == null){
            throw new RuntimeException("系统繁忙，请刷新后重试！");
        }

        DataDictionary dataDictionary0 = dataDictionaryMapper.selectById(dataDictionary.getId());

        if (dataDictionary0 == null){
            throw new RuntimeException("系统繁忙，请刷新后重试！");
        }

        if (!dataDictionary0.getType().equals("A")){
            throw new RuntimeException("系统繁忙，请刷新后重试！");
        }

        LambdaQueryWrapper<DataDictionary> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DataDictionary::getDictionaryName, dictionaryName);
        queryWrapper.eq(DataDictionary::getType, "A");
        queryWrapper.notIn(DataDictionary::getId,dataDictionary.getId());
        List<DataDictionary> list = dataDictionaryMapper.selectList(queryWrapper);
        if (list != null && list.size()>0){
            throw new RuntimeException("“" + dictionaryName + "”类型已经存在，请勿编辑重复的字典类型！");
        }

        LambdaQueryWrapper<DataDictionary> queryWrapper2 = new LambdaQueryWrapper<>();
        queryWrapper2.eq(DataDictionary::getDictionaryCode, dictionaryCode);
        queryWrapper2.eq(DataDictionary::getType, "A");
        queryWrapper2.notIn(DataDictionary::getId,dataDictionary.getId());
        List<DataDictionary> list2 = dataDictionaryMapper.selectList(queryWrapper2);
        if (list2 != null && list2.size()>0){
            throw new RuntimeException("code“" + dictionaryCode + "”类型已经存在，请勿编辑重复的字典code！");
        }

        dataDictionary0.setDictionaryName(dictionaryName);
        dataDictionary0.setDictionaryCode(dictionaryCode);
        if (dataDictionary.getDictionarySort() != null) {
            dataDictionary0.setDictionarySort(dataDictionary.getDictionarySort());
        }
        //dataDictionary0.setOperatorId(WmsCommonUtils.getStringUserInfo(WMS_CCP_USER_INFO_USER_CODE.getCode()));
        dataDictionary0.setUpdateTime(new Date());

        return this.updateById(dataDictionary0);
    }

    @Override
    public boolean delDataDictionaryA(Long id) {

        if (id == null){
            throw new RuntimeException("系统繁忙，请刷新后重试！");
        }

        DataDictionary dataDictionary0 = dataDictionaryMapper.selectById(id);

        if (dataDictionary0 == null){
            throw new RuntimeException("系统繁忙，请刷新后重试！");
        }

        if (!dataDictionary0.getType().equals("A")){
            throw new RuntimeException("系统繁忙，请刷新后重试！");
        }

        int i = dataDictionaryMapper.deleteById(id);

        if (i == 1){
            return true;
        }
        return false;

    }

    @Override
    public boolean addDataDictionaryB(DataDictionary dataDictionary0) {


        String dictionaryName = dataDictionary0.getDictionaryName();
        String dictionaryCode = dataDictionary0.getDictionaryCode();
        Integer parentId = dataDictionary0.getParentId();

        if (dictionaryName == null || "".equals(dictionaryName)){
            throw new RuntimeException("请勿传入空值！");
        }
        if (dictionaryCode == null || "".equals(dictionaryCode)){
            throw new RuntimeException("请勿传入空值！");
        }


        LambdaQueryWrapper<DataDictionary> queryWrapper0 = new LambdaQueryWrapper<>();
        queryWrapper0.eq(DataDictionary::getId, parentId);
        queryWrapper0.eq(DataDictionary::getType, "A");
        List<DataDictionary> list0 = dataDictionaryMapper.selectList(queryWrapper0);
        if (list0 == null || list0.size() == 0){
            throw new RuntimeException("请填入准确的父类类型ID！");
        }

        LambdaQueryWrapper<DataDictionary> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DataDictionary::getParentId, parentId);
        queryWrapper.eq(DataDictionary::getDictionaryName, dictionaryName);
        queryWrapper.eq(DataDictionary::getType, "B");
        List<DataDictionary> list = dataDictionaryMapper.selectList(queryWrapper);
        if (list != null && list.size()>0){
            throw new RuntimeException("“" + dictionaryName + "”类型已经存在，请勿添加重复的字典类型！");
        }

        LambdaQueryWrapper<DataDictionary> queryWrapper2 = new LambdaQueryWrapper<>();
        queryWrapper2.eq(DataDictionary::getParentId, parentId);
        queryWrapper2.eq(DataDictionary::getDictionaryCode, dictionaryCode);
        queryWrapper2.eq(DataDictionary::getType, "B");
        List<DataDictionary> list2 = dataDictionaryMapper.selectList(queryWrapper2);
        if (list2 != null && list2.size()>0){
            throw new RuntimeException("code“" + dictionaryCode + "”已经存在，请勿添加重复的字典code！");
        }

        DataDictionary dataDictionary = new DataDictionary();
        dataDictionary.setType("B");
        dataDictionary.setParentId(parentId);
        dataDictionary.setDictionaryName(dictionaryName);
        dataDictionary.setDictionaryCode(dictionaryCode);
        //dataDictionary.setOperatorId(WmsCommonUtils.getStringUserInfo(WMS_CCP_USER_INFO_USER_CODE.getCode()));
        dataDictionary.setUpdateTime(new Date());
        if (dataDictionary0.getDictionarySort() != null) {
            dataDictionary.setDictionarySort(dataDictionary0.getDictionarySort());
        }else{
            dataDictionary.setDictionarySort(1);
        }

        return this.save(dataDictionary);
    }

    @Override
    public boolean updateDataDictionaryB(DataDictionary dataDictionary) {
        String dictionaryName = dataDictionary.getDictionaryName();
        String dictionaryCode = dataDictionary.getDictionaryCode();
        if (dictionaryName == null || "".equals(dictionaryName)){
            throw new RuntimeException("请勿传入空值！");
        }
        if (dictionaryCode == null || "".equals(dictionaryCode)){
            throw new RuntimeException("请勿传入空值！");
        }
        if (dataDictionary.getId() == null){
            throw new RuntimeException("系统繁忙，请刷新后重试！");
        }

        DataDictionary dataDictionary0 = dataDictionaryMapper.selectById(dataDictionary.getId());

        if (dataDictionary0 == null){
            throw new RuntimeException("系统繁忙，请刷新后重试！");
        }

        if (!dataDictionary0.getType().equals("B")){
            throw new RuntimeException("系统繁忙，请刷新后重试！");
        }

        if (dataDictionary0.getParentId() == null){
            throw new RuntimeException("系统繁忙，请刷新后重试！");
        }

        LambdaQueryWrapper<DataDictionary> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DataDictionary::getParentId, dataDictionary0.getParentId());
        queryWrapper.eq(DataDictionary::getDictionaryName, dictionaryName);
        queryWrapper.eq(DataDictionary::getType, "B");
        queryWrapper.notIn(DataDictionary::getId,dataDictionary.getId());
        List<DataDictionary> list = dataDictionaryMapper.selectList(queryWrapper);
        if (list != null && list.size()>0){
            throw new RuntimeException("“" + dictionaryName + "”类型已经存在，请勿编辑重复的字典类型！");
        }

        LambdaQueryWrapper<DataDictionary> queryWrapper2 = new LambdaQueryWrapper<>();
        queryWrapper2.eq(DataDictionary::getParentId, dataDictionary0.getParentId());
        queryWrapper2.eq(DataDictionary::getDictionaryCode, dictionaryCode);
        queryWrapper2.eq(DataDictionary::getType, "B");
        queryWrapper2.notIn(DataDictionary::getId,dataDictionary.getId());
        List<DataDictionary> list2 = dataDictionaryMapper.selectList(queryWrapper2);
        if (list2 != null && list2.size()>0){
            throw new RuntimeException("code“" + dictionaryCode + "”类型已经存在，请勿编辑重复的字典code！");
        }

        dataDictionary0.setDictionaryName(dictionaryName);
        dataDictionary0.setDictionaryCode(dictionaryCode);
        //dataDictionary0.setOperatorId(WmsCommonUtils.getStringUserInfo(WMS_CCP_USER_INFO_USER_CODE.getCode()));
        if (dataDictionary.getDictionarySort() != null) {
            dataDictionary0.setDictionarySort(dataDictionary.getDictionarySort());
        }
        dataDictionary0.setUpdateTime(new Date());

        return this.updateById(dataDictionary0);
    }

    @Override
    public boolean delDataDictionaryB(Long id) {
        if (id == null){
            throw new RuntimeException("系统繁忙，请刷新后重试！");
        }

        DataDictionary dataDictionary0 = dataDictionaryMapper.selectById(id);

        if (dataDictionary0 == null){
            throw new RuntimeException("系统繁忙，请刷新后重试！");
        }

        if (!dataDictionary0.getType().equals("B")){
            throw new RuntimeException("系统繁忙，请刷新后重试！");
        }

        int i = dataDictionaryMapper.deleteById(id);

        if (i == 1){
            return true;
        }
        return false;
    }

    @Override
    public Map<String, String> getMapByDictionaryCode(String dictionaryCode) {
        Map<String, String> map = new HashMap<>();
        if (dictionaryCode == null || "".equals(dictionaryCode)){
            return map;
        }

        LambdaQueryWrapper<DataDictionary> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DataDictionary::getDictionaryCode, dictionaryCode);
        queryWrapper.eq(DataDictionary::getType, "A");
        DataDictionary dataDictionary = dataDictionaryMapper.selectOne(queryWrapper);
        if (dataDictionary == null){
            return map;
        }

        LambdaQueryWrapper<DataDictionary> queryWrapper2 = new LambdaQueryWrapper<>();
        queryWrapper2.eq(DataDictionary::getParentId, dataDictionary.getId());
        queryWrapper2.eq(DataDictionary::getType, "B");
        queryWrapper2.orderByAsc(DataDictionary::getDictionarySort);
        List<DataDictionary> list = dataDictionaryMapper.selectList(queryWrapper2);
        for (DataDictionary dictionary : list) {
            map.put(dictionary.getDictionaryCode(),dictionary.getDictionaryName());
        }

        return map;
    }

}
